<?php
namespace jy_wechatnews;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function checklogin(){
		cfc('core')->checklogin();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function current_daili_file_types(){
		return str_array($this->setting['daili_file']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function aimsg($tomsg,$isexi=false){
		if(!$this->setting['aitype']){
			exi('请设置《AI创作接口》','error');
		}
		if($this->setting['aitype']==1){
			$msg=model('jy_chatwenxin/model')->getmsg($tomsg,$isexi);
		}elseif($this->setting['aitype']==2){
			$msg=model('jy_xfyunai/model')->getmsg($tomsg,$isexi);
		}elseif($this->setting['aitype']==3){
			$msg=model('jy_chatwenxin/model')->getmsg($tomsg,$isexi);
		}elseif($this->setting['aitype']==4){
			$msg=model('jy_xfyunai/model')->getmsg($tomsg,$isexi);
		}
		return $msg;
	}
	public function add_log($listid=0,$adwei=1,$type=0,$ispay=false){
	    //6小时内访问过
        $lasttime=TIMESTAMP-6*3600;
        $isadd=pdo_count('jy_wechatnews_adlog',array('status'=>1,'listid'=>$listid,'type'=>$type,'adwei'=>$adwei,'mid'=>$_SESSION['uid'],'createtime >'=>$lasttime),'id');
        if($isadd && !$ispay){
           return false;
        }
		$status=1;
		if($type==1){
			$fee=$this->setting['kan_bfee'];
		}elseif($type==3){
			$fee=$this->setting['kan_price'];
			$status=0;
		}
        $log=array(
            'uniacid'=>$_SESSION['uniacid'],
            'mid'=>$_SESSION['uid'],
            'listid'=>$listid,
            'fee'=>$fee,
            'adwei'=>$adwei,
			'type'=>$type,
            'createtime'=>TIMESTAMP,
            'status'=>$status,
        );
        $this->insert('adlog',$log);
        $id=pdo_insertid();
		if($status){
			pdo_update('jy_wechatnews_list',array('readnum +='=>1),array('id'=>$listid,'uniacid'=>$_SESSION['uniacid']));
			if($this->setting['get_bfee']>0 && $type!=2){
				$merchid=pdo_getcolumn('jy_wechatnews_list',array('id'=>$listid,'uniacid'=>$_SESSION['uniacid']),'merchid');
				$mid=pdo_getcolumn('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'mid');
				if($mid){
					model('core/member')->member_credit($mid,'credit3',$this->setting['get_bfee'],true,'jy_wechatnews','奖励商家金币');
				}
			}
			if($type==2 && $this->setting['look_bfee']){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->setting['look_bfee'],true,'jy_wechatnews','看广告赚金币');
			}
		}
        return $id;
    }
	public function add_newslog($listid=0,$type=0,$ispay=false){
	    $item=pdo_get('jy_wechatnews_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$listid));
		$status=1;
		if($type==1){
			$fee=$item['price'];
		}else{
			$fee=$item['jinbi'];
		}
        $log=array(
            'uniacid'=>$_SESSION['uniacid'],
            'mid'=>$_SESSION['uid'],
            'listid'=>$listid,
            'fee'=>$fee,
			'type'=>$type,
			'ispay'=>$ispay,
            'createtime'=>TIMESTAMP,
            'status'=>$ispay?1:0,
        );
        $this->insert('newslog',$log);
		if($status){
			$log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'title'=>'',
				'logtype'=>$type,
				'listid'=>$listid,
				'jinbi'=>!$type?$item['jinbi']:0,
				'price'=>$type?$item['price']:0,
				'merchid'=>$item['merchid'],
				'createtime'=>TIMESTAMP,
			);
			$this->insert('news',$log);
			$newsid=pdo_insertid();
			return $newsid;
		}
        return 0;
    }
	public function order_pay($logid=0,$post=array()){
		$adlog=pdo_get('jy_wechatnews_adlog',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'ispay'=>1,
			'status'=>1,
			'paytype'=>$post['paytype'],
			'transid'=>$post['transid'],
		);
		$this->pdo_update('adlog',$update,array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_wechatnews_list',array('readnum +='=>1),array('id'=>$adlog['listid'],'uniacid'=>$_SESSION['uniacid']));
		if($this->setting['get_bfee']>0){
			$merchid=pdo_getcolumn('jy_wechatnews_list',array('id'=>$adlog['listid'],'uniacid'=>$_SESSION['uniacid']),'merchid');
			$mid=pdo_getcolumn('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'mid');
			if($mid){
				model('core/member')->member_credit($mid,'credit3',$this->setting['get_bfee'],true,'jy_wechatnews','奖励商家金币');
			}
		}
		return true;
	}
}